﻿
<div ms-controller="tmpmodelview">
    <div class="control-group">
        <label class="control-label v-top">
            <b style="color: #f00;">*</b>
            角色名：
        </label>
        <div class="controls">
            <input type="text" name="schoolname" class="input-xlarge input-xfat" ms-duplex="modelData.RoleName" data-rules="required" maxlength="200">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label v-top">
            权限：
        </label>
    </div>
    <div>
        <table class="sui-table table-bordered authtable" ms-repeat-el="RoleAuth">
            <tbody>
                <tr style="background-color:#e2dddd;">
                    <td>
                        <label class="checkbox-pretty inline menutxt1" ms-class="checked:IsChecked1(el)" ms-attr-mid="el.ID">
                            <input type="checkbox"><span>{{el.MenuTxt}}</span>
                        </label>
                    </td>
                </tr>
                <tr ms-repeat-im="el.SubItem">
                    <td>
                        <label class="checkbox-pretty inline menutxt2" ms-class="checked:IsChecked1(im)" ms-attr-mid="im.ID">
                            <input type="checkbox"><span>{{im.MenuTxt}}</span>
                        </label>

                        <label class="checkbox-pretty inline menutxtck" mth="ADD" ms-if="IsAuth(im,'ADD')" ms-class="checked:IsChecked2(im,'ADD')">
                            <input type="checkbox"><span>添加</span>
                        </label>
                        <label class="checkbox-pretty inline menutxtck" mth="EDIT" ms-if="IsAuth(im,'EDIT')" ms-class="checked:IsChecked2(im,'EDIT')">
                            <input type="checkbox" ><span>编辑</span>
                        </label>
                        <label class="checkbox-pretty inline menutxtck" mth="DEL" ms-if="IsAuth(im,'DEL')" ms-class="checked:IsChecked2(im,'DEL')">
                            <input type="checkbox"><span>删除</span>
                        </label>
                        <label class="checkbox-pretty inline menutxtck" mth="UPDOWN" ms-if="IsAuth(im,'UPDOWN')" ms-class="checked:IsChecked2(im,'UPDOWN')">
                            <input type="checkbox"><span>上下架</span>
                        </label>
                        <label class="checkbox-pretty inline menutxtck" mth="HIDE" ms-if="IsAuth(im,'HIDE')" ms-class="checked:IsChecked2(im,'HIDE')">
                            <input type="checkbox"><span>显示</span>
                        </label>
                    </td>

                </tr>

            </tbody>
        </table>
    </div>
</div>

<script type="text/javascript">
    var tempmodel = avalon.define({
        $id: "tmpmodelview",
        modelData: { "ID": "0", "RoleName": "", "RoleAuth": "" },
        RoleAuth: [],
        RoleAuthH:[],
        ClientData: [],
        CatData1: [],
        Init: function (ID) {
            if (ID) {
                $.getJSON("/adapi/auth/rolemodel/" + ID, function (r) {
                    if (r.error == "") {
                        tempmodel.modelData = r.result;
                        if (r.result.RoleAuth) {
                            tempmodel.RoleAuthH = JSON.parse(r.result.RoleAuth);
                        }
                    }

                })
            }

            $.getJSON("/adapi/auth/roleauth", function (r) {
                if (r.error == "") {
                    tempmodel.RoleAuth = r.result;
                }

            })
        },
        IsAuth: function (el,op) {
            var isV = false;
            if (el.MenuAuth && el.MenuAuth.indexOf(op) > -1) {
                isV = true;
            }
            return isV;
        },
        IsChecked1: function (el) {
            var isV = false;

            tempmodel.RoleAuthH.forEach(function (item, index) {
                if (item.MenuID == el.ID) {
                        isV = true;
                        return true;
                }
            })
            return isV;
        },
        IsChecked2:function(el,op){
            var isV = false;

            tempmodel.RoleAuthH.forEach(function (item, index) {
                if (item.MenuID == el.ID) {
                    if (item.RoleAuth.indexOf(op) > -1) {
                        isV = true;
                        return true;
                    }
                }
            })
            return isV;

        },
        tmpSaveData: function () {
            var authRole = [];
            $(".authtable").each(function () {

                if ($(this).find(".menutxt1").hasClass("checked")) {
                    authRole.push({ "MenuID": $(this).find(".menutxt1").attr("mid"), "RoleAuth": "" });
                }

                $(this).find(".menutxt2").each(function (index, item) {
                    if ($(this).hasClass("checked")) {
                        var mid = $(this).attr("mid");
                        var mth = "";
                        $(this).parent().find(".menutxtck").each(function () {
                            if ($(this).hasClass("checked")) {
                                if (mth == "") {
                                    mth = $(this).attr("mth");
                                } else {
                                    mth += "," + $(this).attr("mth");
                                }
                               
                            }
                        })
                        authRole.push({ "MenuID": mid, "RoleAuth": mth });
                    }
                })
            })
            tempmodel.modelData.RoleAuth = JSON.stringify(authRole);

            $.post("/adapi/auth/roleadd", tempmodel.modelData.$model, function (r) {
                if (r.error == "") {
                    top.ComFunJS.winShowMsg("操作成功");
                    top.ComFunJS.close();
                } else {
                    top.ComFunJS.winShowAlert(r.error);
                }

            })
        }

    });


</script>
